package xcmg.device.web.logistic;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.enums.BillDesc;
import xcmg.device.service.crm.util.ActionResultUtil;
import xcmg.device.service.logistic.LogisticWayBillService;
import xcmg.device.service.vo.logistic.BusinessOrderSelectedInfoVO;
import xcmg.device.service.vo.logistic.LogisticBillwayPrintVO;
import xcmg.device.service.vo.logistic.LogisticWayBilDeliveryConfirmVO;
import xcmg.device.service.vo.logistic.LogisticWayBillAuditVO;
import xcmg.device.service.vo.logistic.LogisticWayBillListQueryParamVO;
import xcmg.device.service.vo.logistic.LogisticWayBillListQueryResultVO;
import xcmg.device.service.vo.logistic.LogisticWayBillModifyVO;
import xcmg.device.service.vo.logistic.LogisticWayBillSaveVO;
import xcmg.device.service.vo.logistic.LogisticWayBillViewInfoVO;
import xcmg.device.service.vo.logistic.SelectBusinessBillResultVO;
import xcmg.device.service.vo.logistic.SelectBusinessBillsQueryParamVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Collections;

/**
 * @Des 运单管理controller
 * @Date 2021/4/23 11:34
 * @Author wangzhaoyu
 * @Param 
 * @Return 
 */
@RestController
@RequestMapping("/logisticWayBill")
public class LogisticWayBillController extends BasicController {

    @Autowired
    private LogisticWayBillService logisticWayBillService;

    /**
     * @Des 查询物流运单列表
     * @Date 2021/5/18 10:51
     * @Author wangzhaoyu
     * @Param [condition]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @PostMapping("/listing")
    @FastMappingInfo(needLogin = true)
    public ActionResult listing(@RequestBody SearchCommonVO<LogisticWayBillListQueryParamVO> condition) throws Exception {
        String orgId = getOrgId();
        PageCommonVO<LogisticWayBillListQueryResultVO> pageResult = logisticWayBillService.listingLogisticWayBills(orgId, condition);
        return ActionResultUtil.successActionResult(pageResult.getPageInfo());
    }

    /**
     * @Des 选择业务创建物流运单的发运交接单或三包出库单
     * @Date 2021/4/27 9:51
     * @Author wangzhaoyu
     * @Param []
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @PostMapping("/selectBusinessBills")
    @FastMappingInfo(needLogin = true)
    public ActionResult selectBusinessBills(@RequestBody SearchCommonVO<SelectBusinessBillsQueryParamVO> condition) throws Exception {
        SelectBusinessBillsQueryParamVO filters = condition.getFilters();
        if(filters == null){
            return ActionResultUtil.errorActionResult(ErrorCode.IllegalArument);
        }
        PageCommonVO<SelectBusinessBillResultVO> page = null;
        String businessType = filters.getBusinessType();
        if(StringUtils.isEmpty(businessType)){
            page = new PageCommonVO<>();
            page.setPageInfo(new PageInfo(Collections.EMPTY_LIST));
            page.setPageInfoList(Collections.EMPTY_LIST);
            return ActionResultUtil.successActionResult(page.getPageInfo());
        }
        String orgId = getOrgId();
        filters.setOrgId(orgId);
        if(BillDesc.DeliveryReceiptNo.getDesc().equals(businessType)){
            //发运交接单逻辑
            page = logisticWayBillService.receiptBusinessOrders(condition);
        }else if(BillDesc.ThreeOut.getDesc().equals(businessType)){
            //三包出库单逻辑
            page = logisticWayBillService.threeOutBusinessOrders(condition);
        }else{
            page = new PageCommonVO<>();
            page.setPageInfo(new PageInfo(Collections.EMPTY_LIST));
            page.setPageInfoList(Collections.EMPTY_LIST);
        }
        return ActionResultUtil.successActionResult(page.getPageInfo());
    }

    /**
     * @Des  创建物流运单，勾选发运交接单/三包出库单后，查询出已选择的这些单据的展示信息
     * @Date 2021/5/7 16:44
     * @Author wangzhaoyu
     * @Param [businessType : 单据类型  XGFJ=发运交接单，XGSC=三包出库单]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @RequestMapping(value = "/businessOrderSelectedInfo" , method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult businessOrderSelectedInfo(String businessType,String[] businessOrderNos) throws Exception {
        if(StringUtils.isEmpty(businessType)){
            return ActionResultUtil.errorActionResult(ErrorCode.IllegalArument.getCode(),"请指定单据类型");
        }
        String orgId = getOrgId();
        if(businessType.equals(BillDesc.ThreeOut.getDesc())){//三包出库单,
            BusinessOrderSelectedInfoVO selectedInfo = logisticWayBillService.threeGuaranteesOutsSelectedInfo(businessOrderNos,orgId);
            return ActionResultUtil.successActionResult(selectedInfo);
        }else if(businessType.equals(BillDesc.DeliveryReceiptNo.getDesc())){//发运交接单
            BusinessOrderSelectedInfoVO selectedInfo = logisticWayBillService.deliveryReceiptsSelectedInfo(businessOrderNos);
            return ActionResultUtil.successActionResult(selectedInfo);
        }else{
            return ActionResultUtil.errorActionResult(ErrorCode.IllegalArument.getCode(),"仅支持三包出库单和发运交接单");
        }
    }




    /**
     * @Des 保存物流运单
     * @Date 2021/5/7 15:39
     * @Author wangzhaoyu
     * @Param []
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @PostMapping("/saveLogistics")
    @FastMappingInfo(needLogin = true)
    public ActionResult saveLogistics(@RequestBody LogisticWayBillSaveVO logisticWayBillSaveVO) throws Exception {
        String orgId = getOrgId();
        String userId = getUserId();
        String userName = getUserName();
        LogisticWayBillSaveVO.HeadInfo headInfo = logisticWayBillSaveVO.getHeadInfo();
        String id = headInfo.getId();
        if(StringUtils.isEmpty(id)){//新增操作
            logisticWayBillService.addLogistic(logisticWayBillSaveVO,orgId,userId,userName,0);
        }else{// 更新操作
            logisticWayBillService.updateLogistic(logisticWayBillSaveVO,0);
        }
        return ActionResultUtil.successActionResult();
    }

    /**
     * @Des 提交物流运单
     * @Date 2021/5/13 16:06
     * @Author wangzhaoyu
     * @Param []
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @PostMapping("/submitLogistics")
    @FastMappingInfo(needLogin = true)
    public ActionResult submitLogistics(@RequestBody LogisticWayBillSaveVO logisticWayBillSaveVO) throws Exception {
        String orgId = getOrgId();
        String userId = getUserId();
        String userName = getUserName();
        LogisticWayBillSaveVO.HeadInfo headInfo = logisticWayBillSaveVO.getHeadInfo();
        String id = headInfo.getId();
        if(StringUtils.isEmpty(id)){//新增操作
            logisticWayBillService.addLogistic(logisticWayBillSaveVO,orgId,userId,userName,1);
        }else{// 更新操作
            logisticWayBillService.updateLogistic(logisticWayBillSaveVO,1);
        }
        return ActionResultUtil.successActionResult();
    }

    /**
     * @Des 编辑操作时获取物流运单原信息
     * @Date 2021/5/14 8:58
     * @Author wangzhaoyu
     * @Param [id]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @GetMapping("/editViewInfo")
    @FastMappingInfo(needLogin = true)
    public ActionResult editViewInfo(String id){
        if(StringUtils.isEmpty(id)){
            return ActionResultUtil.errorActionResult(ErrorCode.IllegalArument);
        }
        LogisticWayBillSaveVO result = logisticWayBillService.editViewInfo(id);
        return ActionResultUtil.successActionResult(result);
    }

    /**
     * @Des 点击物流运单号查看详情
     * @Date 2021/5/14 14:26
     * @Author wangzhaoyu
     * @Param [id]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @GetMapping("/viewInfo")
    @FastMappingInfo(needLogin = true)
    public ActionResult viewInfo(String id){
        if(StringUtils.isEmpty(id)){
            return ActionResultUtil.errorActionResult(ErrorCode.IllegalArument);
        }
        LogisticWayBillViewInfoVO result = logisticWayBillService.viewInfo(id);
        return ActionResultUtil.successActionResult(result);
    }
    
    /**
     * @Des 物流运单执行变更操作时展示的页面信息
     * @Date 2021/5/18 16:08
     * @Author wangzhaoyu
     * @Param []
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @GetMapping("/modifyViewInfo")
    @FastMappingInfo(needLogin = true)
    public ActionResult modifyViewInfo(String id){
        LogisticWayBillModifyVO result = logisticWayBillService.modifyViewVO(id);
        if(result == null){
            return ActionResultUtil.errorActionResult(ErrorCode.Failure.getCode(),"未查询到指定记录");
        }
        return ActionResultUtil.successActionResult(result);
    }

    /**
     * @Des 变更物流运单保存按钮
     * @Date 2021/5/18 16:25
     * @Author wangzhaoyu
     * @Param [modify]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @PostMapping("/modify/save")
    @FastMappingInfo(needLogin = true)
    public ActionResult modifySave(@RequestBody LogisticWayBillModifyVO modifyParam) throws Exception {
        //只有运输方式为专车且单据状态为已保存、审核拒绝时
        logisticWayBillService.modify(modifyParam,1);
        return ActionResultUtil.successActionResult();
    }


    /**
     * @Des 变更物流运单提交按钮
     * @Date 2021/5/18 16:25
     * @Author wangzhaoyu
     * @Param [modify]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @PostMapping("/modify/submit")
    @FastMappingInfo(needLogin = true)
    public ActionResult modifySubmit(@RequestBody LogisticWayBillModifyVO modifyParam) throws Exception {
        //只有运输方式为专车且单据状态为已保存、审核拒绝时
        logisticWayBillService.modify(modifyParam,0);
        return ActionResultUtil.successActionResult();
    }

    /**
     * @Des 物流运单删除接口
     * @Date 2021/5/18 18:03
     * @Author wangzhaoyu
     * @Param []
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @GetMapping("/remove")
    public ActionResult remove(String id) throws Exception {
        logisticWayBillService.remove(id);
        return ActionResultUtil.successActionResult();
    }

    /**
     * @Des 审核通过
     * @Date 2021/5/18 19:18
     * @Author wangzhaoyu
     * @Param [audit]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @PostMapping("/auditPass")
    @FastMappingInfo(needLogin = true)
    public ActionResult auditPass(@RequestBody LogisticWayBillAuditVO audit) throws Exception {
        String userId = getUserId();
        String userName = getUserName();
        logisticWayBillService.audit(audit,userId,userName,true);
        return ActionResultUtil.successActionResult();
    }

    /**
     * @Des 审核拒绝
     * @Date 2021/5/18 19:18
     * @Author wangzhaoyu
     * @Param [audit]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @PostMapping("/auditReject")
    @FastMappingInfo(needLogin = true)
    public ActionResult auditReject(@RequestBody LogisticWayBillAuditVO audit) throws Exception {
        String userId = getUserId();
        String userName = getUserName();
        logisticWayBillService.audit(audit,userId,userName,false);
        return ActionResultUtil.successActionResult();
    }

    /**
     * @Des 确认发货
     * @Date 2021/5/18 19:54
     * @Author wangzhaoyu
     * @Param []
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @PostMapping("/deliveryConfirm")
    @FastMappingInfo(needLogin = true)
    public ActionResult deliveryConfirm(@RequestBody LogisticWayBilDeliveryConfirmVO confirmParam) throws Exception {
        String userId = getUserId();
        String userName = getUserName();
        logisticWayBillService.deliveryConfirm(confirmParam,userId,userName);
        return ActionResultUtil.successActionResult();
    }

    /**
     * @Des 物流运单打印
     * @Date 2021/5/22 13:44
     * @Author wangzhaoyu
     * @Param []
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @GetMapping("/printWayBillInfo")
    @FastMappingInfo(needLogin = true)
    public ActionResult printWayBillInfo(String id) throws Exception {
        LogisticBillwayPrintVO result = logisticWayBillService.printLogisticBillway(id);
        return ActionResultUtil.successActionResult(result);
    }

}
